package com.iceflame.zxingane
{
	import flash.display.BitmapData;
	import flash.events.EventDispatcher;
	import flash.events.StatusEvent;
	import flash.external.ExtensionContext;
	import flash.system.System;
	import flash.utils.getTimer;
	
	[Event(name="decode_complete", type="com.iceflame.zxingane.ZXingEvent")]

	public class ZXingAne extends EventDispatcher
	{
		private static const DEBUG:String = "debug";
		
		private static const ERROR:String = "error";
		
		private static const DECODE:String = "decode";
		
		private var context:ExtensionContext;
		
		public function ZXingAne()
		{
			context = ExtensionContext.createExtensionContext("com.iceflame.zxingane.ZXingAne","");
			context.addEventListener(StatusEvent.STATUS,onStatusEvent);
		}
		
		/** 编码图片  可手动指定编码为图片的尺寸  尺寸越小速度越高 */
		public function encode(value:String,encodeW:int = 64,encodeH:int = 64):BitmapData{
			var bmdArr:Array = context.call("encode",encodeW,encodeH,value) as Array;
			var w:int = bmdArr[0];
			var h:int = bmdArr[1];
			var bmd:BitmapData = new BitmapData(w,h,false,0xffffffff);
			for (var y:int = 0; y < h; y++) {
				var offset:int = y * w;
				for (var x:int = 0; x < w; x++) {
					var index:int = offset + x + 2;
					var isB:Boolean = bmdArr[index];
					bmd.setPixel(x,y,isB ? 0x000000 : 0xffffff);
				}
			}
			return bmd;
		}
		
		/** 解码图片  传入一张位图数据  尺寸越小的图片解码效率越高  */
		public function decode(bmd:BitmapData):String{
			var w:int = bmd.width;
			var h:int = bmd.height;
			var bmdArr:Array = new Array(w * h);
			for (var y:int = 0; y < h; y++) {
				var offset:int = y * w;
				for (var x:int = 0; x < w; x++) {
					var index:int = offset + x;
					bmdArr[index] = bmd.getPixel(x,y);
				}
			}
			var str:String = context.call("decode",w,h,bmdArr) as String;
			return str;
		}
		
		/** 以异步方式解码  此操作会立即返回  待解码完毕后会抛出 QRCodeEvent.DECODE_COMPLETE 事件  尺寸越小  效率越高 */
		public function sycDecode(bmd:BitmapData):void{
			var w:int = bmd.width;
			var h:int = bmd.height;
			var bmdArr:Array = new Array(w * h);
			for (var y:int = 0; y < h; y++) {
				var offset:int = y * w;
				for (var x:int = 0; x < w; x++) {
					var index:int = offset + x;
					bmdArr[index] = bmd.getPixel(x,y);
				}
			}
			context.call("sycdecode",w,h,bmdArr);
		}
		
		/** 读出相机支持的预览尺寸  **/
		public function getCameraMode():Vector.<PreviewSize>{
			var sizes:Vector.<PreviewSize> = new Vector.<PreviewSize>();
			var str:String = context.call("getcameramode") as String;
			if(str != null && str != ""){
				var arr:Array = str.split(",");
				for each(var mode:String in arr){
					var dimPosition:int = mode.indexOf('x');
					var w:int = parseInt(mode.substring(0, dimPosition));
					var h:int = parseInt(mode.substring(dimPosition + 1));
					var pre:PreviewSize = new PreviewSize(w,h);
					sizes.push(pre);
				}
			}
			return sizes;
		}
		
		/** 打开相机 设置预览图尺寸  传入显示文字 */
		public function openZXing(preview:PreviewSize = null,title:String = ""):void{
			var w:int = preview == null ? 0 : preview.width;
			var h:int = preview == null ? 0 : preview.height;
			context.call("openzxing",w,h,title);
		}
		
		
		private function onStatusEvent(event:StatusEvent):void{
			trace(event.code + ":" + event.level);
			if(event.code == DEBUG){
				trace(event.code + ":" + event.level);
			}else if(event.code == ERROR){
				throw new Error(event.level);
			}else if(event.code == DECODE){
				var qrce:ZXingEvent;
				if(event.level == "null"){
					qrce = new ZXingEvent(
						ZXingEvent.DECODE_COMPLETE,null);
				}else{
					var arr:Array = event.level.split("=%|%=");
					if(arr && arr.length == 2){
						var type:String = arr[0];
						var code:String = arr[1];
						qrce = new ZXingEvent(
							ZXingEvent.DECODE_COMPLETE,code,type);
					}else{
						qrce = new ZXingEvent(
							ZXingEvent.DECODE_COMPLETE,event.level);
					}
				}
				this.dispatchEvent(qrce);
			}
		}
		
	}
}